package com.mastercard.labs

import com.sun.org.apache.bcel.internal.generic.NEW;
import org.hibernate.criterion.Order

class UserService {
	def searchUserByCondition(username, nric, name, status){
		def result = User.createCriteria().list {
			if(username){
				eq("username", username)
			}
			if(nric){
				eq("nric", nric)
			}
			if(name){
				ilike("name", "%"+name+"%")
			}
			if(status){
				eq("status", User.Status.valueOf(status))
			}
		}
		return result
	}
	
	def searchUserForDashboard(){
		def result = User.createCriteria().list {
			createAlias("roles", "rol")
			ne("rol.name", "admin")
			ne("rol.name", "ADMIN")
			order("loginDate", "desc")
			maxResults(5)
		}
		return result
	}
	
	def getUserById(id){
		return User.findById(id)
	}
	
	def saveUser(user){
		user.save()
	}
	
	def savaUserActivation(userActivation){
		userActivation.save()
	}
}
